package org.finesys.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.finesys.common.core.constants.RestApiConfig;
import org.finesys.common.core.constants.ValidationGroup;
import org.finesys.common.core.module.R;
import org.finesys.common.core.util.ValidationUtil;
import org.finesys.system.api.dto.AppDto;
import org.finesys.system.api.entity.SysApp;
import org.finesys.system.constants.ControllerRestApiConfig;
import org.finesys.system.convert.SysAppConvert;
import org.finesys.system.service.SysAppService;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "应用管理")
@RestController
@RequestMapping(ControllerRestApiConfig.APP)
@RequiredArgsConstructor
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysAppController {
    private final SysAppService sysAppService;

    /**
     * 分页
     */
    @Operation(summary = "分页")
    @GetMapping(value = RestApiConfig.PAGE)
    public R<IPage<AppDto>> pageSysApp(Page<AppDto> page, AppDto appDto) {
        ValidationUtil.validateParam(appDto, ValidationGroup.query.class);
        return R.success(sysAppService.pageApp(page, appDto));
    }

    /**
     * 添加
     */
    @Operation(summary = "添加")
    @PostMapping(value = RestApiConfig.ADD)
    public R<Void> add(@RequestBody AppDto appDTO) {
        ValidationUtil.validateParam(appDTO, ValidationGroup.add.class);
        return R.judge(sysAppService.saveApp(appDTO));
    }

    /**
     * 修改
     */
    @Operation(summary = "修改")
    @PutMapping(value = RestApiConfig.UPDATE)
    public R<Void> update(@RequestBody AppDto appDTO) {
        ValidationUtil.validateParam(appDTO, ValidationGroup.update.class);
        return R.judge(sysAppService.updateApp(appDTO));
    }

    /**
     * 删除
     */
    @Operation(summary = "删除")
    @DeleteMapping(value = RestApiConfig.DELETE)
    public R<Void> delete(Long id) {
        return R.judge(sysAppService.deleteApp(id));
    }

    /**
     * 获取单条
     */
    @Operation(summary = "获取单条")
    @GetMapping(value = RestApiConfig.GET)
    public R<AppDto> findById(Long id) {
        return R.success(sysAppService.findById(id));
    }

    /**
     * 获取全部
     */
    @Operation(summary = "获取全部")
    @GetMapping(value = RestApiConfig.ALL)
    public R<List<AppDto>> findAll() {
        List<SysApp> list = sysAppService.findAll();
        return R.success(SysAppConvert.INSTANCE.convertList(list));
    }

}
